package com.ruoyi.crm.service.impl;

import com.ruoyi.crm.domain.DictData;
import com.ruoyi.crm.mapper.DictDataMapper;
import com.ruoyi.crm.service.DictDataService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 字典数据表(DictData)表服务实现类
 *
 * @author makejava
 * @since 2022-06-24 11:12:34
 */
@Service("dictDataService")
public class DictDataServiceImpl implements DictDataService {
	@Resource
	private DictDataMapper dictDataMapper;

	/**
	 * 通过ID查询单条数据
	 *
	 * @param dictCode 主键
	 * @return 实例对象
	 */
	@Override
	public DictData queryById(Long dictCode) {
		return this.dictDataMapper.queryById(dictCode);
	}

	/**
	 * 分页查询
	 *
	 * @param dictData    筛选条件
	 * @param pageRequest 分页对象
	 * @return 查询结果
	 */
	@Override
	public Page<DictData> queryByPage(DictData dictData, PageRequest pageRequest) {
		long total = this.dictDataMapper.count(dictData);
		return new PageImpl<>(this.dictDataMapper.queryAllByLimit(dictData, pageRequest), pageRequest, total);
	}

	/**
	 * 新增数据
	 *
	 * @param dictData 实例对象
	 * @return 实例对象
	 */
	@Override
	public DictData insert(DictData dictData) {
		this.dictDataMapper.insert(dictData);
		return dictData;
	}

	/**
	 * 修改数据
	 *
	 * @param dictData 实例对象
	 * @return 实例对象
	 */
	@Override
	public DictData update(DictData dictData) {
		this.dictDataMapper.update(dictData);
		return this.queryById(dictData.getDictCode());
	}

	/**
	 * 通过主键删除数据
	 *
	 * @param dictCode 主键
	 * @return 是否成功
	 */
	@Override
	public boolean deleteById(Long dictCode) {
		return this.dictDataMapper.deleteById(dictCode) > 0;
	}
}
